package com.jhws.business.property.controller;


import com.jhws.business.property.bo.PropertyConstant;
import com.jhws.business.property.bo.PropertyOrgnazation;
import com.jhws.business.property.bo.PropertyStaff;
import com.jhws.business.property.bo.PropertyStaffOrg;
import com.jhws.business.property.service.PropertyService;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import com.jhws.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/property/org")
public class PropertyOrgController {
    @Resource
    PropertyService propertyService;
    @Resource
    ITblUserDimService IUserService;

    @RequestMapping("getList.x")
    @ResponseBody
    public JsonRespData getList(String commCode) {
        if (StringUtils.isNotEmpty(commCode))
            return new JsonRespData("", 1, propertyService.findOrgByWhere(" comm_code = '" + commCode + "'"));

        return new JsonRespData("", 1, propertyService.findOrgByWhere(" comm_code = '' and owner = '" + IUserService.currentUser().getLogName() + "'"));
    }

    @RequestMapping("update.x")
    @ResponseBody
    public JsonRespData update(PropertyOrgnazation org, Long staffId, PropertyStaff staff) {

        TblUserDimBo user = IUserService.currentUser();
        org.setOwner(user.getLogName());
        propertyService.save(org);

        if (staffId == null) {
            staff.setOwner(org.getOwner());
            staff.setStaffType(PropertyConstant.POSITION_MANGER);
            propertyService.save(staff);
        } else
            staff.setId(staffId);

        List<PropertyStaffOrg> propertyStaffOrgs = propertyService.findStaffOrgBindgByWhere("staff_id = '" + staff.getId() + "' and org_id = '" + org.getId() + "'");
        if (propertyStaffOrgs == null || propertyStaffOrgs.size() == 0) {
            PropertyStaffOrg staffOrg = new PropertyStaffOrg();
            staffOrg.setStaffId(staff.getId());
            staffOrg.setOrgId(org.getId());
            propertyService.save(staffOrg);
        }
        return new JsonRespData("", 1, "");
    }


    @RequestMapping("delete.x")
    @ResponseBody
    public JsonRespData delete(Long id) throws Exception {
        List<PropertyOrgnazation> list = propertyService.findOrgByWhere("parent_id = '" + id + "'");
        if (list != null) {
            for (PropertyOrgnazation org : list) {
                /* 假如该组织存在子组织,则递归删除子组织 */
                this.delete(org.getId());
            }
        }

        List<PropertyStaffOrg> staffOrgs = propertyService.findStaffOrgBindgByWhere("org_id = '" + id + "'");
        if (staffOrgs != null) {
            for (PropertyStaffOrg staffOrg : staffOrgs) {
                /* 删除该组织下所有员工关系，及该组织本身 */
                propertyService.deleteStaffOfOrg(staffOrg.getId(), staffOrg.getStaffId());
                propertyService.deleteOrg(id);
            }
        }
        return new JsonRespData("", 1, "");
    }
}
